package org.etherframework.tests.features.mediator
{
	import org.etherframework.common.IContractual;
	import org.etherframework.core.IModule;
	import org.etherframework.mediator.IMediator;
	import org.etherframework.mediator.IMediators;
	import org.etherframework.tests.features.mediator.mediators.ManuallyCreatedViewMediator;
	import org.etherframework.tests.features.mediator.views.ManuallyCreatedView;
	import org.etherframework.tests.utils.ModuleInfo;
	import org.etherframework.tests.utils.TestUtility;
	import org.flexunit.async.Async;
	import org.fluint.uiImpersonation.UIImpersonator;
	import org.hamcrest.assertThat;
	import org.hamcrest.object.equalTo;
	import org.hamcrest.object.isFalse;
	import org.hamcrest.object.isTrue;
	import org.hamcrest.object.notNullValue;
	import org.hamcrest.object.nullValue;
	
	import spark.components.Group;

	public class when_creating_mediators_for_view_outside_module
	{	
		private static var _testUtility:TestUtility = new TestUtility();
		private static var _module:IModule;
		private static var _m:IMediators;
		private static var _manuallyCreatedView:ManuallyCreatedView;
		private static var _manuallyCreatedView2:ManuallyCreatedView;
		private static var _manuallyCreatedViewMediator:ManuallyCreatedViewMediator;
		private static var _moduleView:Group;
		private static var _info:ModuleInfo;
		private static var _rootView:Group;
		
		[BeforeClass(async)]
		public static function setUpBeforeClass():void
		{
			_rootView = UIImpersonator.addChild(new Group()) as Group;
			_moduleView = _rootView.addElement(new Group()) as Group;
			_info = _testUtility.createModuleInfo(null, _moduleView);
			_module = _info.module;
			_m = _module.mediators;
			
			_m.addMediator(ManuallyCreatedView, ManuallyCreatedViewMediator, IContractual);
			
			_manuallyCreatedView = new ManuallyCreatedView();
			_manuallyCreatedView2 = new ManuallyCreatedView();
			
			_manuallyCreatedViewMediator = _m.createMediator(_manuallyCreatedView) as ManuallyCreatedViewMediator;
			
			_rootView.addElement(_manuallyCreatedView);
			_rootView.addElement(_manuallyCreatedView2);
			Async.delayCall(when_creating_mediators_for_view_outside_module, function():void{}, 50);
		}
		
		[AfterClass]
		public static function tearDownAfterClass():void
		{
			_testUtility.dispose();
			UIImpersonator.removeAllChildren();
		}
		
		[Test(order=1)]
		public function first_manually_created_mediator_should_be_used():void
		{
			var mediator:IMediator = _m.hasMediator(_manuallyCreatedView);
			
			assertThat(_manuallyCreatedViewMediator.destroyed, isFalse());
			assertThat(_info.controllers.hasController(_manuallyCreatedViewMediator), notNullValue());
			assertThat(mediator, equalTo(_manuallyCreatedViewMediator));
		}
		
		[Test(order=2)]
		public function automatically_created_mediator_should_not_be_used():void
		{
			var mediator:IMediator = _m.hasMediator(_manuallyCreatedView2);
			
			assertThat(mediator, nullValue());
		}
		
		[Test(order=3)]
		public function view_should_have_proper_contract_assigned():void
		{
			assertThat(_manuallyCreatedView.contract, equalTo(_manuallyCreatedViewMediator));
		}
		
		[Test(order=4)]
		public function correct_mediator_persistence_should_be_applied():void
		{
			assertThat(_info.controllers.hasController(_manuallyCreatedViewMediator), notNullValue());
		}
	}
}